﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ADA.Entity;
using ADA.Business;
using System.IO;

namespace ADA.Presentation.PRL.MarketNews
{
    public partial class EditRiceForm : Form
    {
        bool add;
        int riceID;
        MainForm main;
        bool flag;
        public EditRiceForm(MainForm Main)
        {
            main = Main;
            add = true;
            InitializeComponent();
        }

        public EditRiceForm(int RiceID, MainForm Main)
        {
            main = Main;
            add = false;
            riceID = RiceID;
            InitializeComponent();
        }

        private void EditRiceForm_Load(object sender, EventArgs e)
        {
            lblError.Text = "";
            if (add == true)
            {
                btnUpload.Visible = false;
                return;
            }
            string message;
            Rice rice;
            BOLNews bolNews = new BOLNews();
            if (bolNews.getRiceInfo(riceID, out rice, out message) == true)
            {
                tbxTypeName.Text = rice.Name;
                tbxTypeNameJP.Text = rice.NameJP;
                img.ImageLocation = Common.URL_RICE + Path.GetFileName(rice.Image.ToString().Trim());
                tbxOrigin.Text = rice.Origin;
                tbxOriginJP.Text = rice.OriginJP;
                tbxDescription.Text = rice.Description;
                tbxDescriptionJP.Text = rice.DescriptionJP;
            }
            else
            {
                MessageBox.Show(message);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            lblError.Text = "";
            validate();
            if (flag == false) return;
            if (add == true)
            {
                addRice();
            }
            else
            {
                editRice();
            }
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            openFileDialog.Multiselect = false;
            DialogResult result = openFileDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                img.ImageLocation = openFileDialog.FileName;
            }
        }

        private void addRice()
        {
            Rice rice = new Rice();
            rice.Name = tbxTypeName.Text.Trim();
            rice.NameJP = tbxTypeNameJP.Text.Trim();
            if (rice.NameJP.Equals("")) rice.NameJP = rice.Name;

            string pathImg = "";
            pathImg = Common.URL_SAVE_RICE + Common.DEFAULT_RICE_IMAGE_NAME;
            rice.Image = pathImg;
            
            rice.Origin = tbxOrigin.Text.Trim();
            rice.OriginJP = tbxOriginJP.Text.Trim();
            if (rice.OriginJP.Equals("")) rice.OriginJP = rice.Origin;
            rice.Description = tbxDescription.Text.Trim();
            rice.DescriptionJP = tbxDescriptionJP.Text.Trim();
            if (rice.DescriptionJP.Equals("")) rice.DescriptionJP = rice.Description;
            rice.AdminID = main.ad.ID;

            string message;
            BOLNews bolNews = new BOLNews();
            if (bolNews.addRice(rice, out message) == true)
            {
                main.LoadTabPage_RiceInfomation();
                this.Close();
            }
            else
            {
                lblError.Text = message;
            }
        }

        private void editRice()
        {
            Rice rice = new Rice();
            rice.ID = riceID;
            rice.Name = tbxTypeName.Text.Trim();
            rice.NameJP = tbxTypeNameJP.Text.Trim();
            if (rice.NameJP.Equals("")) rice.NameJP = rice.Name;

            string path = Common.URL_RICE + Path.GetFileName(img.ImageLocation);
            string pathImg = Common.URL_SAVE_RICE + Path.GetFileName(img.ImageLocation);
            if (openFileDialog.FileName.Equals("") == false)
            {
                pathImg = Common.URL_SAVE_RICE + rice.ID + Path.GetExtension(openFileDialog.FileName);
                path = Common.URL_RICE + rice.ID + Path.GetExtension(openFileDialog.FileName);
                img.Image.Save(path);
            }
            img.ImageLocation = path;
            rice.Image = pathImg;
            rice.Origin = tbxOrigin.Text.Trim();
            rice.OriginJP = tbxOriginJP.Text.Trim();
            if (rice.OriginJP.Equals("")) rice.OriginJP = rice.Origin;
            rice.Description = tbxDescription.Text.Trim();
            rice.DescriptionJP = tbxDescriptionJP.Text.Trim();
            if (rice.DescriptionJP.Equals("")) rice.DescriptionJP = rice.Description;
            rice.AdminID = main.ad.ID;

            string message;
            BOLNews bolNews = new BOLNews();
            if (bolNews.editRice(rice, out message) == true)
            {
                main.LoadTabPage_RiceInfomation();
                this.Close();
            }
            else
            {
                lblError.Text = message;
            }
        }

        private void validate()
        {
            flag = true;
            if ((tbxTypeName.Text.Trim() == ""))
            {
                errorName.SetError(tbxTypeName, Common.MESSAGE_ERROR_MUST_INPUT_DATA);
                flag = false;
            }
            else
            {
                errorName.Clear();
            }
            if (openFileDialog.FileName.Equals("") == false)
            {
                string ext = Path.GetExtension(openFileDialog.FileName);
                if (BOLCommon.checkFileImage(ext) == false)
                {
                    errorImage.SetError(img, Common.MESSAGE_ERROR_NOT_BE_IMAGE);
                    flag = false;
                }
                else
                {
                    errorImage.Clear();
                }
            }
            else
            {
                errorImage.Clear();
            }
            if ((tbxOrigin.Text.Trim() == ""))
            {
                errorOrigin.SetError(tbxOrigin, Common.MESSAGE_ERROR_MUST_INPUT_DATA);
                flag = false;
            }
            else
            {
                errorOrigin.Clear();
            }
            if ((tbxDescription.Text.Trim() == ""))
            {
                errorDescription.SetError(tbxDescription, Common.MESSAGE_ERROR_MUST_INPUT_DATA);
                flag = false;
            }
            else
            {
                errorDescription.Clear();
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void tbxTypeName_TextChanged(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }
    }
}
